package cn.bsuc.infirmary.service.impl;


import cn.bsuc.infirmary.domain.Systemdictionary;
import cn.bsuc.infirmary.domain.Systemdictionaryitem;
import cn.bsuc.infirmary.mapper.SystemdictionaryMapper;
import cn.bsuc.infirmary.mapper.SystemdictionaryitemMapper;
import cn.bsuc.infirmary.query.QueryObject;
import cn.bsuc.infirmary.query.SystemDictionaryItemQueryObject;
import cn.bsuc.infirmary.service.ISystemdictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemdictionaryServiceImpl implements ISystemdictionaryService {
    @Autowired
    private SystemdictionaryMapper systemdictionaryMapper;
    @Autowired
    private SystemdictionaryitemMapper itemMapper;

    @Override
    public void saveOrUpdate(Systemdictionary s) {
        if (s.getId() == null) {
            systemdictionaryMapper.insert(s);
        } else {
            systemdictionaryMapper.updateByPrimaryKey(s);
        }
    }

    @Override
    public void saveOrUpdateItem(Systemdictionaryitem s) {
        //查看是否填序号,没有填的话就获取最大值在加一
        if (s.getSequence() == null) {
            Integer maxSequence = itemMapper.selectMaxSequence(s.getParentId());
            s.setSequence(maxSequence + 1);
        }
        if (s.getId() == null) {
            itemMapper.insert(s);
        } else {
            itemMapper.updateByPrimaryKey(s);
        }
    }

    @Override
    public void deleteItem(Long id) {
        itemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Systemdictionaryitem selectOneItem(Long id) {
        return itemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Systemdictionaryitem> selectListItem() {
        return itemMapper.selectAll();
    }

    @Override
    public PageInfo<Systemdictionaryitem> selectQueryObjectItem(SystemDictionaryItemQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Systemdictionaryitem> list = itemMapper.selectForList(qo);
        return new PageInfo<Systemdictionaryitem>(list);
    }

    @Override
    public List<Systemdictionaryitem> selectBySn(String sn) {
        return itemMapper.selectBySn(sn);
    }

    @Override
    public void delete(Long id) {
        //先删明细再删目录
        itemMapper.deleteByParentId(id);
        systemdictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Systemdictionary selectOne(Long id) {
        return systemdictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Systemdictionary> selectList() {
        return systemdictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<Systemdictionary> selectQueryObject(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Systemdictionary> list = systemdictionaryMapper.selectForList(qo);
        return new PageInfo<Systemdictionary>(list);
    }
}
